﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace WxSDK.Common
{
    /// <summary>
    /// 缓存系统 HttpRuntime.Cache 
    /// </summary>
    public static class WeChatCache
    {
        private static Cache cache = HttpRuntime.Cache;
        
        /// <summary>
        /// 插入缓存值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Insert(string key, object value)
        {
            cache.Insert(key, value);
        }
        /// <summary>
        /// 获取当前值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValue(string key)
        {
            return cache.Get(key);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        public static void Del(string key)
        {
            cache.Remove(key);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Update(string key, object value)
        {
            cache.Insert(key, value);
        }
        /// <summary>
        /// 设置绝对过期时间缓存  当超过设定时间，立即移除。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout">过期时间 单位 : 秒</param>
        public static void Insert_NoSliding(string key, object value, long timeout)
        {
            cache.Insert(key, value, null, DateTime.Now.AddSeconds(timeout), Cache.NoSlidingExpiration);
        }
        /// <summary>
        /// 设置优先级的过期缓存 过期即释放
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <param name="rank">GetRank 调用</param>
        public static void Insert_NoSliding_Rank(string key, object value, long timeout, CacheItemPriority rank)
        {
            cache.Insert(key, value, null, DateTime.Now.AddSeconds(timeout), Cache.NoSlidingExpiration, rank, null);
        }
        /// <summary>
        /// 设置滑动过期时间缓存 当超过设定时间没再使用时，才移除缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout">过期时间 单位 : 秒 注意 时间不能超过一年</param>
        public static void Insert_NoAbsolute(string key, object value, long timeout)
        {
            cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(timeout));
        }
        /// <summary>
        /// 设置优先级的滑动过期缓存 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <param name="rank"></param>
        public static void Insert_NoAbsolute_Rank(string key, object value, long timeout, CacheItemPriority rank)
        {
            cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(timeout), rank, null);
        }

        /// <summary>
        /// 获取缓存设置的优先级
        /// Low = 1 //在服务器释放系统内存时，具有该优先级级别的缓存项比分配了 System.Web.Caching.CacheItemPriority.Normal
        /// BelowNormal = 2 //在服务器释放系统内存时，具有该优先级级别的缓存项很有可能被从缓存删除，其被删除的可能性仅次于具有 System.Web.Caching.CacheItemPriority.Low
        /// Normal = 3 //缓存项优先级的默认值为 System.Web.Caching.CacheItemPriority.Normal
        /// AboveNormal = 4 //在服务器释放系统内存时，具有该优先级级别的缓存项最不可能被从缓存删除。
        /// High = 5 //在服务器释放系统内存时，具有该优先级级别的缓存项将不会被自动从缓存删除。但是，具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除
        /// NotRemovable = 6 最高优先级
        /// </summary>
        /// <param name="rank">优先级ID 1~6</param>
        /// <returns></returns>
        public static CacheItemPriority GetRank(int rank)
        {
            CacheItemPriority cacheitempriority = CacheItemPriority.Default;
            switch (rank)
            {
                case 1:
                    cacheitempriority = CacheItemPriority.Low;
                    break;
                case 2:
                    cacheitempriority = CacheItemPriority.BelowNormal;
                    break;
                case 3:
                    cacheitempriority = CacheItemPriority.Normal;
                    break;
                case 4:
                    cacheitempriority = CacheItemPriority.AboveNormal;
                    break;
                case 5:
                    cacheitempriority = CacheItemPriority.High;
                    break;
                case 6:
                    cacheitempriority = CacheItemPriority.NotRemovable;
                    break;
                default:
                    cacheitempriority = CacheItemPriority.Normal;
                    break;
            }
            return cacheitempriority;
        }


    }
}
